package com.jinkaimen.goodcash.ui.activity;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.ht.baselibs.application.HTApplication;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.ReportAccoutGetBean;
import com.jinkaimen.goodcash.constant.ConstantPool;
import com.jinkaimen.goodcash.manager.ApiManager;

import retrofit2.Call;
import retrofit2.Response;

public class LoginActivity extends BaseActivity {

    private CheckBox mSeePwdView;
    private EditText mPwdEditText;
    private EditText mTelView;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setLeftBtnVisibility(View.GONE)
                .setTitleText("登录")//
                .setTitleTextColor(R.color.black)
                .setBackgroundColor(getResources().getColor(R.color.white));
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initChildView() {
        mTelView = (EditText) findViewById(R.id.et_activity_register_tel);
        mPwdEditText = (EditText) findViewById(R.id.et_activity_register_pwd);
        mSeePwdView = (CheckBox) findViewById(R.id.cb_activity_register_see_pwd);
    }

    @Override
    protected void initChildData() {

        if (HTApplication.IS_DEBUG_MODE){
            mTelView.setText("admin");
            mPwdEditText.setText("jkmadmin123");
        }

    }

    @Override
    protected void setChildViewListener() {
        mSeePwdView.setOnClickListener(this);
        findViewById(R.id.btn_activity_login).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        String tel = mTelView.getText().toString();
        String pwd = mPwdEditText.getText().toString();
        switch (v.getId()){
            case R.id.cb_activity_register_see_pwd:
                if (mSeePwdView.isChecked()){
                    mPwdEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.btn_activity_login:
                if (TextUtils.isEmpty(tel)){
                    UIUtils.showToast("请输入用户名");
                    return;
                }

                if (TextUtils.isEmpty(pwd)){
                    UIUtils.showToast("请输入密码");
                    return;
                }

                showLoadingDialog();

                ApiManager.getApi().reportLogin(
                        "INV_HSY001009",
                        tel,
                        pwd
                ).enqueue(new NetCallBack<ReportAccoutGetBean>() {
                    @Override
                    protected void onSuccess(Call<ReportAccoutGetBean> call, Response<ReportAccoutGetBean> response) {
                        ReportAccoutGetBean reportAccoutGetBean = response.body();
                        SharedPreferencesUtils.put(ConstantPool.REPORT_ACCOUT_ID, reportAccoutGetBean.adminUser.id);
                        ActivityUtils.startActivityAndFinish(LoginActivity.this, ReportMainActivity.class);
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
        }
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    public void onEvent(EventCenter eventCenter) {
        switch (eventCenter.eventCode){
            case EventCode.EVENT_CODE_START_LOGIN:
                ActivityUtils.finishActivity(this);
                break;
        }
    }
}
